package sr.myserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sr.myserver.commons.Result;
import sr.myserver.entity.SysMenus;
import sr.myserver.entity.SysRoleMenu;
import sr.myserver.entity.SysRoles;
import sr.myserver.exception.RunException;
import sr.myserver.mapper.SysMenusMapper;
import sr.myserver.mapper.SysRoleMenuMapper;
import sr.myserver.mapper.SysRolesMapper;
import sr.myserver.service.SysRolesService;
import sr.myserver.vo.SelectData;
import sr.myserver.vo.SysRolesEdit;
import sr.myserver.vo.common.Basic;
import sr.myserver.vo.common.PageRequest;
import sr.myserver.vo.common.PageResponse;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限信息
 *
 * @author sirui
 **/
@Service
public class SysRolesServiceImpl implements SysRolesService {
    @Autowired
    SysRolesMapper sysRolesMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysMenusMapper sysMenusMapper;

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 实体对象
     */
    @Override
    public SysRoles get(Serializable id) {
        return sysRolesMapper.selectById(id);
    }

    /**
     * 增加
     *
     * @param entity 请求参数
     * @return 响应码和响应消息
     */
    @Override
    @Transactional(rollbackFor = RunException.class)
    public Basic.Response add(SysRolesEdit entity) throws RunException {
        sysRolesMapper.insert(entity);
        List<String> menuIds = entity.getMenuIds();
        menuIds.forEach(id -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(entity.getId());
            roleMenu.setMenuId(id);
            SysMenus menus = sysMenusMapper.selectById(id);
            if (menus == null) {
                return;
            }
            roleMenu.setButtonIds(menus.getBtnId());
            sysRoleMenuMapper.insert(roleMenu);
        });
        return Result.ok();
    }

    /**
     * 修改
     *
     * @param entity 请求参数
     * @return 响应码和响应消息
     */
    @Override
    @Transactional(rollbackFor = RunException.class)
    public Basic.Response update(SysRolesEdit entity) throws RunException {
        List<String> menuIds = entity.getMenuIds();
        UpdateWrapper<SysRoleMenu> wrapper = new UpdateWrapper<>();
        wrapper.notIn("menu_id", menuIds);
        wrapper.eq("role_id", entity.getId());
        sysRoleMenuMapper.delete(wrapper);

        menuIds.forEach(menuId -> {
            QueryWrapper<SysRoleMenu> query = new QueryWrapper<>();
            query.eq("role_id", entity.getId());
            query.eq("menu_id", menuId);
            SysRoleMenu roleMenu = sysRoleMenuMapper.selectOne(query);
            if (roleMenu == null) {
                roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(entity.getId());
                roleMenu.setMenuId(menuId);
                SysMenus menus = sysMenusMapper.selectById(menuId);
                if (menus == null) {
                    return;
                }
                roleMenu.setButtonIds(menus.getBtnId());
                sysRoleMenuMapper.insert(roleMenu);
            }
        });
        sysRolesMapper.updateById(entity);
        return Result.ok();
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 响应码和响应消息
     */
    @Override
    @Transactional(rollbackFor = RunException.class)
    public Basic.Response deleteById(Serializable id) throws RunException {
        UpdateWrapper<SysRoleMenu> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id", id);
        sysRoleMenuMapper.delete(wrapper);
        sysRolesMapper.deleteById(id);
        return Result.ok();
    }

    /**
     * 列表查询
     *
     * @param request 请求参数
     * @return 列表数据集合
     */
    @Override
    public PageResponse<SysRoles> queryTables(PageRequest request) {
        IPage<SysRoles> data = sysRolesMapper.queryTables(request.getPage(), request.getQueryData());
        return new PageResponse(data.getCurrent(), data.getSize(), data.getTotal(), data.getRecords());
    }

    /**
     * 下拉查询
     *
     * @param request 请求参数
     * @return 列表数据集合
     */
    @Override
    public PageResponse<SelectData> querySelect(PageRequest request) {
        IPage<SelectData> data = sysRolesMapper.querySelect(request.getPage(), request.getQueryData());
        return new PageResponse(data.getCurrent(), data.getSize(), data.getTotal(), data.getRecords());
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 实体对象
     */
    @Override
    public SysRolesEdit getEdit(String id) {
        SysRoles sysRoles = get(id);
        QueryWrapper<SysRoleMenu> query = new QueryWrapper<>();
        query.eq("role_id", id);
        List<SysRoleMenu> data = sysRoleMenuMapper.selectList(query);
        List<String> menuIds = new ArrayList<>();
        data.forEach(item -> {
            menuIds.add(item.getMenuId());
        });
        SysRolesEdit result = new SysRolesEdit();
        result.setId(sysRoles.getId());
        result.setName(sysRoles.getName());
        result.setMenuIds(menuIds);
        return result;
    }
}